#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 5e5 + 10;
const int mod = 1e9 + 7;
struct node {
  int num, id;
  bool operator<(const node &b) const { return num < b.num; }
} a[N];
int c[N], b[N], n;
ll ans;
int inline lowbit(int x) { return x & -x; }
void update(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}
int query(int x) {
  int res = 0;
  while (x > 0) {
    res += c[x];
    x -= lowbit(x);
  }
  return res;
}
int main() {
  while (scanf("%d", &n) != EOF) {
    if (n == 0) break;
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i].num);
      a[i].id = i;
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) b[a[i].id] = i;
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      update(b[i], 1);
      ans += 1LL * i - query(b[i]);
    }
    cout << ans << endl;
  }
}
